<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
    input{display: block;}
    </style>
</head>
<body>
        id:<input type="text" name="" id="id">
        name:<input type="text" name="" id="name">
        img:<input type="text" name="" id="img">
        num:<input type="text" name="" id="num">
        price:<input type="text" name="" id="price">
        <input type="submit" id="btn">
        <script>
        var btninp=document.getElementById("btn")
        var nameinp=document.getElementById("name")
        var imginp=document.getElementById("img")
        var numinp=document.getElementById("num")
        var idinp=document.getElementById("id")
        var priceinp=document.getElementById("price")
        btninp.onclick=function(){
            // 判断该key对应的值是否存在，如果存在把数量加到原来的数量上面
            if(localStorage.getItem("goods_"+idinp.value)){
                var obj=JSON.parse(localStorage.getItem("goods_"+idinp.value))
                 obj.goodsnum=obj.goodsnum*1+numinp.value*1
                 localStorage.setItem("goods_"+idinp.value,JSON.stringify(obj))
            }
            else{
            var obj={}
            obj.goodsid=idinp.value;
            obj.goodsname=nameinp.value;
            obj.goodsimg=imginp.value
            obj.goodsnum=numinp.value
            obj.goodsprice=priceinp.value
            console.log(obj)
            localStorage.setItem("goods_"+idinp.value,JSON.stringify(obj))
            }
        }
          
        </script>
</body>
</html>